package com.hao.DaoImpl;

import com.hao.Dao.productjsqDao;
import com.hao.Dao.staffDao;
import com.hao.Dao.workDao;
import com.hao.entity.work;
import com.hao.util.BaseDao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018/8/8.
 */
public class workImpl implements workDao {
    @Override
    public List<work> find() {
        List<work> list=new ArrayList<work>();
        String sql="SELECT * FROM work ";
        Connection conn=null;
        PreparedStatement state=null;

        ResultSet rs=null;
        try {
            conn= BaseDao.getConnection();
            state=conn.prepareStatement(sql);
            rs=state.executeQuery();

            productjsqDao dao1=new productjsqImpl();
            staffDao dao=new staffImpl();

            while(rs.next()){
                work nn=new work();
                nn.setWorkid(rs.getInt("workid"));
                nn.setStaffid(rs.getInt("staffid"));
                nn.setUserName(rs.getString("userName"));
                nn.setUserPhone(rs.getString("userPhone"));
                nn.setUserAddress(rs.getString("userAddress"));
                nn.setWorkType(rs.getInt("workType"));
                nn.setStatus(rs.getInt("status"));
                nn.setWpid(rs.getInt("wpid"));
                nn.setWlist(dao1.finduser(rs.getInt("wpid")));
                nn.setSlist(dao.findstaff(rs.getInt("staffid")));
                list.add(nn);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeConnection(conn, state, rs);
        }
        return list;
    }

    @Override
    public int dele(int wid) {
        String sql="DELETE FROM work WHERE workid=?";
        Object[] ob={
                wid
        };
        return BaseDao.Update(sql, ob);
    }

    @Override
    public int add(work add) {
        String sql="INSERT into work VALUES(null,?,?,?,?,?,?,?)";
        Object[] ob={
               add.getStaffid(),
                add.getUserName(),
                add.getUserPhone(),
                add.getUserAddress(),
                add.getWorkType(),
                add.getStatus(),
                add.getWpid()

        };
        return BaseDao.Update(sql, ob);
    }
}
